# -*- coding: utf-8 -*-
# @Time : 2018/12/24 14:09
# @Author : Administrator
# @File : read_data.py
# @Project : python_demo
# @title : read data
import xlrd


class Common():
    def read_data(self,filename):
        with open(filename) as f:
            content=f.readlines()
            key1=content[0].split(",")[0]
            key2 = content[0].split(",")[1]
            key3 = content[0].split(",")[2]
            key4 = content[0].split(",")[3].strip()

            list=[]
            for i in range(1,len(content)):
                # print(i)
                # print(content[i].split(","))
                dict={}
                dict[key1]=content[i].strip().split(",")[0]
                dict[key2] = content[i].strip().split(",")[1]
                dict[key3] = content[i].strip().split(",")[2]
                dict[key4] = content[i].strip().split(",")[3]
                list.append(dict)
            # print(list)
            return list

    def read_excel(self,filename,index=0):
            work_book=xlrd.open_workbook(filename)
            work_sheet=work_book.sheets()[index]
            rows=work_sheet.nrows
            cols=work_sheet.ncols
            key1=work_sheet.cell(0,0).value
            key2=work_sheet.cell(0,1).value
            key3 = work_sheet.cell(0, 2).value
            key4 = work_sheet.cell(0, 3).value
            key5 = work_sheet.cell(0, 4).value
            key6 = work_sheet.cell(0, 5).value
            list=[]
            for i in range(1,work_sheet.nrows):
                # print(work_sheet.row_values(i)[0])
                customername=work_sheet.row_values(i)[0]
                childsex = work_sheet.row_values(i)[1]
                childdate = work_sheet.row_values(i)[2]
                creditkids = work_sheet.row_values(i)[3]
                creditcloth = work_sheet.row_values(i)[4]
                expect=work_sheet.row_values(i)[5]
                dict={}
                dict[key1]=customername
                dict[key2] = childsex
                dict[key3] = childdate
                dict[key4] = creditkids
                dict[key5] = creditcloth
                dict[key6] = expect
                list.append(dict)
            print(list)
            return list

if __name__ == '__main__':
    # Common().read_data("testdata.csv")
    Common().read_excel("data.xls")
